<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Application Execute Tree</title>
    <link rel="stylesheet" href="../dashboard/css/element-index.css">
    <!-- <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"> -->
    <style>
        .el-header, .el-footer {
            background-color: #B3C0D1;
            color: #333;
            text-align: center;
            line-height: 60px;
        }

        .el-aside {
            background-color: #D3DCE6;
            color: #333;
            height: 1000px;
        }

        .el-main {
            background-color: #E9EEF3;
            color: #333;
            height: 1000px;
            width: auto;
        }

        body > .el-container {
            margin-bottom: 40px;
        }

        .demo-table-expand {
            font-size: 0;
        }
        .demo-table-expand label {
            width: 90px;
            color: #99a9bf;
        }
        .demo-table-expand .el-form-item {
            margin-right: 0;
            margin-bottom: 0;
            width: 50%;
        }
    </style>
</head>

<!-- import Vue before Element -->
<script src="../dashboard/js/vue.js"></script>
<!--<script src="https://unpkg.com/vue/dist/vue.js"></script>-->
<!-- import JavaScript -->
<script src="../dashboard/js/element-index.js"></script>
<!--<script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
<!-- axios -->
<script src="../dashboard/js/axios.min.js"></script>
<!--<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>-->
<!-- json.js -->
<script src="../dashboard/js/json.js"></script>


<body>
<div id="app">
    <el-container>
        <!-- Script描述信息 -->
        <el-header>
            <el-link type="primary" :underline="false">ApiTouch : </el-link>
            <el-link type="danger" :underline="false">{{header_touch}}</el-link>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside width="auto">
                <el-tree
                        :data="dirTree"
                        :props="defaultProps"
                        :highlight-current="true"
                        :default-expand-all="true"
                        accordion
                        @node-contextmenu="tryLoadApi">
                </el-tree>
            </el-aside>
            <!-- 主API信息 -->
            <el-main>

                <template>
                    <el-table :data="clsApi" style="width: 100%">

                        <!-- 前扩展列 -->
                        <el-table-column type="expand">
                            <template slot-scope="props">
                                <!-- 参数 -->
                                <template>
                                    <el-table :data="props.row.params" stripe style="width: 100%">
                                        <el-table-column prop="paramIdx" label="参数索引" ></el-table-column>
                                        <el-table-column prop="paramClassName" label="参数类型" ></el-table-column>
                                        <el-table-column prop="paramDoc" label="参数文档" ></el-table-column>
                                        <el-table-column prop="paramValue" label="参数值" >
                                            <template slot-scope="scope">
                                                <el-input
                                                        type="textarea"
                                                        autosize
                                                        placeholder="nil"
                                                        v-show="true"
                                                        v-model="scope.row.paramValue">
                                                </el-input>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </template>
                                <!-- 执行结果 -->
                                <el-input
                                        type="textarea"
                                        autosize
                                        placeholder="Result will display here."
                                        v-model="props.row.response">
                                </el-input>
                            </template>
                        </el-table-column>

                        <!-- 基础列 -->
                        <el-table-column label="类信息" prop="className"></el-table-column>
                        <el-table-column label="API" prop="methodName"></el-table-column>
                        <el-table-column label="API文档" prop="functionDescription"></el-table-column>
                        <el-table-column label="执行策略" width="120">
                            <template slot-scope="slotStrategy">
                                <template>
                                    <el-select v-model="slotStrategy.row.strategy" placeholder="执行策略">
                                        <el-option
                                                v-for="strategyItem in strategyOptions"
                                                :key="strategyItem.value"
                                                :label="strategyItem.label"
                                                :value="strategyItem.value">
                                        </el-option>
                                    </el-select>
                                </template>
                            </template>
                        </el-table-column>
                        <el-table-column label="Operate" width="120">
                            <template slot-scope="exec">
                                <el-button type="success" @click.native.prevent="executeApi(exec.row)">Execute</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>


            </el-main>
        </el-container>
    </el-container>


</div>


<script>
    var RELATION_ROOT_PATH = "../..";
    var vueData = {
        //当前Header-Touch节点
        header_touch:"",
        //目录树
        defaultProps:{ label:"label", children:"children"},
        dirTree:[],
        //API信息
        clsApi:[],
        //执行策略选项
        strategyOptions:[{
            value: 0,
            label: 'Local'
        }, {
            value: 1,
            label: 'Cluster'
        }],
        //执行结果通知选项
        asertResOptions:[
            {title:'Success', type:'success', message:'执行成功'},
            {title:'Biz error', type:'error', message:'业务执行存在异常'},
            {title:'Server error',type:'error', message:'服务接口/参数异常'}
        ]

    };
    var app =new Vue({
        el: '#app',
        data: vueData,
        mounted:function () {
            axios.post(RELATION_ROOT_PATH + "/dashboard/tree/mappings").then(function (response) {
                console.log(response.data);
                vueData.dirTree = response.data;
            }).catch(error => alert(error));
        },
        methods:{
            //预加载API校验
            tryLoadApi: function(data,node,tree){
                var clsMetadata  = node.cls;
                if(clsMetadata == undefined){
                    alert("Not a children Node.");
                }else {
                    app.loadClsApi(clsMetadata);
                }
            },
            //加载API
            loadClsApi:function (clsMetadata) {
                alert(clsMetadata)
                // load class api.
                axios.post(RELATION_ROOT_PATH + "/dashboard/tree/clsApi?clsMetadata=" + clsMetadata).then(function (response) {
                    console.log(response.data);
                    vueData.clsApi = response.data;
                    vueData.header_touch = clsMetadata;
                }).catch(error => alert(error));
            },
            //执行API
            executeApi:function (row) {
                row.response = null;
                axios.post(RELATION_ROOT_PATH + "/dashboard/tree/execute",row).then(function (response) {
                    var serverResponse = response.data;
                    //存在调度结果，判断告警业务异常 / 调度结果格式化展示.
                    if(serverResponse != null && serverResponse != undefined){
                        //业务异常判断并提示
                        if(serverResponse.success != null && serverResponse.success == true){
                            //业务成功
                            app.asertResult(vueData.asertResOptions[0]);
                        }else {
                            //业务异常
                            app.asertResult(vueData.asertResOptions[1]);
                        }
                        serverResponse = formatJson(serverResponse);
                        row.response = serverResponse;
                        console.log(JSON.stringify(row));
                    }
                }).catch(error => {
                    row.response = error.toString();
                    console.log(error.toString());
                    app.asertResult(vueData.asertResOptions[2]);
                });
            },
            asertResult(asertOption) {
                this.$notify(asertOption);
            }
        }
    })
</script>

</body>
</html>